package com.jsh.controller;

import com.jsh.pojo.Admin;
import com.jsh.service.AdminService;
import com.jsh.util.CodeUtil;
import com.jsh.util.RandomValidateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

@Controller
public class AdminController {
    @Autowired
    AdminService adminService;
    @RequestMapping("/")
    public String index(){
        return "login";
    }
    @RequestMapping("/fail")
    public String fail(String name,String password){ return "fail";}
    @RequestMapping("/login")
    public String hello(String name, String password, @RequestParam("code")String code1, Model model, HttpSession session){

        String code=(String)session.getAttribute("verifyCode");

        if (name==null||password==null||code1==null){
            model.addAttribute("msg","用户或者密码或验证码为空");
            return "login";
        }else if (name!=null&&password!=null){
            Admin admin=adminService.findByNameAndPassword(name,password);
            System.out.println(code);
            System.out.println(code1);
            System.out.println(code.equalsIgnoreCase(code1));
            if (admin!=null&&code.equalsIgnoreCase(code1)){
                session.setAttribute("key",name);
                model.addAttribute("msg1",name);
                return "index";
            }else {
                model.addAttribute("msg","请再次检查");

            }
            return "login";
        }
        return "login";
    }
    @RequestMapping("/loginGetVerifyCode")
    public void generate(HttpServletResponse response, HttpSession session) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        CodeUtil codeUtil = new CodeUtil();
        String verifyCodeValue = codeUtil.drawImg(output);
        System.out.println(verifyCodeValue);
        session.setAttribute("verifyCode", verifyCodeValue);
        try {
            ServletOutputStream out = response.getOutputStream();
            output.writeTo(out);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

   @RequestMapping("/login/toInsert")
   public String toInsert(){
        return "addAdmin";
   }
   @RequestMapping("/login/insert")
   public String Insert(Admin admin){
        adminService.insert(admin);
        return "redirect:/";
   }


    /**
     * 获取生成验证码显示到 UI 界面
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping(value="/loginCheckCode")
    public void checkCode(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //设置相应类型,告诉浏览器输出的内容为图片
        response.setContentType("image/jpeg");

        //设置响应头信息，告诉浏览器不要缓存此内容
        response.setHeader("pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);

        RandomValidateCode randomValidateCode = new RandomValidateCode();
        System.out.println(randomValidateCode.getRandomString());
        try {
            randomValidateCode.getRandcode(request, response);//输出图片方法
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
